package com.sofwin.yyb.controller;

import com.sofwin.yyb.entity.User;
import com.sofwin.yyb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String toLogin(){
        return "redirect:/app/login.html";
    }
    @RequestMapping("/index")
    public String toIndex(){
        return "redirect:/app/index.html";
    }
    @RequestMapping("/register")
    public String toRegister(){
        return "redirect:/app/register.html";
    }

    /**
     * 用户登录，登录成功进入首页
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/userLogin")
    public String userLogin(User user,HttpSession session,String code){
        String yzm = (String)session.getAttribute("code");
        if(yzm.equals(code)){
            User uu = userService.findByUser(user);
            if(uu!=null){
                session.setAttribute("user",uu);
                return "redirect:/app/index.html";
            }else{
                return "redirect:/app/login.html";
            }
        }else{
            return "redirect:/app/login.html";
        }

    }

    /**
     * 查看用户信息
     * @param session
     * @return
     */
    @RequestMapping("/account")
    public String account(HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user!=null){
            return "redirect:/app/uc.html";
        }else{
            return "redirect:/app/login.html";
        }
    }
    @ResponseBody
    @RequestMapping("/userInfo")
    public User userInfo(HttpSession session){
        User user = (User)session.getAttribute("user");
        return user;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/app/login.html";
    }

    /**
     * 修改用户信息
     * @param nickname
     * @param session
     * @return
     */
    @RequestMapping("/newNickname")
    public String newNickname(String nickname, HttpSession session){
        User user = (User)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        map.put("id",user.getId());
        map.put("nickname",nickname);
        //修改用户昵称
        userService.updateByNickname(map);
        //将修改后的用户信息重新放在session中
        User user1 = userService.findById(user.getId());
        session.setAttribute("user",user1);
        return "redirect:/app/setting.html";
    }
    @RequestMapping("/newPhone")
    public String newPhone(String phone, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("phone", phone);
        //修改用户手机号码
        userService.updateByPhone(map);
        //将修改后的用户信息重新放在session中
        User user1 = userService.findById(user.getId());
        session.setAttribute("user",user1);
        return "redirect:/app/setting.html";
    }
    @RequestMapping("/newPassword")
    public String newPassword(String password, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("password", password);
        //修改用户密码
        userService.updateByPassword(map);
        return "redirect:/app/login.html";
    }
    @RequestMapping("/set_avatar")
    @ResponseBody
    public Map<String,Object> setAvatar(HttpSession session, @RequestParam(value = "file",required = false)MultipartFile file){
        User user = (User)session.getAttribute("user");
        //获取文件名称
        String url = "../img/"+file.getOriginalFilename();
        Map<String,Object> map = new HashMap<>();
        map.put("id",user.getId());
        map.put("avatar",url);
        //修改头像
        userService.updateByAvatar(map);
        //修改完用户信息，将用户信息重新放在session中
        User u = userService.findById(user.getId());
        session.setAttribute("user",u);
        Map<String,Object> map1 = new HashMap<>();
        map1.put("code",0);
        return map1;
    }
    /**
     * 用户退出登录
     * @return
     */
    @RequestMapping("/logout")
    public String userLogout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/app/login.html";
    }
}
